import {handleActions} from 'redux-actions';
import * as homeTypes from '../../utils/type/HomeTypes';

const defaultStatus = {
    page: 0,
    datas: [],
    banners: [],
    isRefreshing: false,
    isLoading: false,
    isEnd: false
};

export default handleActions({
    [homeTypes.FETCH_HOME_BANNNER_DONE]: (state, action) => {
        return {
            ...state,
            banners: action.payload
        }
    },
    [homeTypes.FETCH_HOME_ARTUICLE_DOING]: (state, action) => {
        let isr= action.payload;
        return {
            ...state,
            isRefreshing:isr,
            isLoading:!isr
        }
    },
    [homeTypes.FETCH_HOME_ARTUICLE_DONE]: (state, action) => {
        const data = action.payload;
        let datas = state.datas;
        if (data.curPage === 1) {
            datas = data.datas;
        } else {
            datas = [...datas, ...data.datas];
        }
        let page = 0;
        //是否为最后一页数据
        isEnd = data.curPage >= data.pageCount;
        if (!isEnd) {
            page = data.curPage;
        } else {
            page = data.pageCount - 1;
        }
        return {
            ...state,
            page: page,
            isRefreshing: false,
            isLoading:false,
            datas: datas
        }
    }
}, defaultStatus);